package com.lp.guoguo.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import javax.sql.DataSource;

/**
 * Quartz 定时任务配置类
 */
@Configuration
public class QuartzConfig {
    
    @Autowired
    private DataSource dataSource;
    
    /**
     * 定时任务调度器工厂
     * Spring Boot 3 会自动配置大部分参数，我们只需要针对性地修改个别参数
     */
    @Bean
    public SchedulerFactoryBean schedulerFactoryBean() {
        SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
        
        // 使用项目的数据源，而不是额外的 C3P0 数据源
        schedulerFactoryBean.setDataSource(dataSource);
        
        // 设置自动启动
        schedulerFactoryBean.setAutoStartup(true);
        
        // 设置延时启动
        schedulerFactoryBean.setStartupDelay(5);
        
        // 设置调度器名称
        schedulerFactoryBean.setSchedulerName("GuoguoScheduler");
        
        // 等待任务完成再关闭
        schedulerFactoryBean.setWaitForJobsToCompleteOnShutdown(true);
        
        // 覆盖已存在的任务
        schedulerFactoryBean.setOverwriteExistingJobs(true);
        
        return schedulerFactoryBean;
    }
} 